package com.bss.controller;

import com.bss.pojo.Customer;
import com.bss.pojo.Order;
import com.bss.pojo.Product;
import com.bss.pojo.Sales;
import com.bss.service.CustomerService;
import com.bss.service.OrderService;
import com.bss.service.ProductService;
import com.bss.service.SalesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.entity.Financial;
import com.sm.entity.UserInfo;
import com.sm.entity.UserLogin;
import com.sm.service.FinancialService;
import com.sm.service.Impl.FinancialServiceImpl;
import com.sm.utils.AttendanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author 袁飞
 * @company 中原工学院
 * @create 2020-03-14-19:49
 */
@RestController
@RequestMapping("/bss/sales")
public class SalesController {
    @Autowired
    SalesService SalesService;
    @Autowired
    ProductService productService;
    @Autowired
    CustomerService customerService;
    @Autowired
    OrderService orderService;
    @RequestMapping("find")
    public Sales getSales(@RequestParam("id") Integer id){
        return SalesService.findSalesById(id);
    }
    @RequestMapping("Upd")
    public void Upd(Sales sales){
        SalesService.updateSales(sales);
    }
    @RequestMapping("add")
    public void add(String productName,String saleName,String saleCount) throws Exception {
        System.out.println(productName+"------"+saleName+"------"+saleCount);
        //减去库存
        Product product = productService.findProductByName(productName);
        product.setProductStock(product.getProductStock()-Integer.valueOf(saleCount));
        productService.updateProduct(product);
        Customer customer = customerService.findByName(saleName);
        Sales sales = new Sales();
        sales.setSaleProduct(productName);
        sales.setSaleId(System.currentTimeMillis()/1000+"");
        sales.setSaleName(saleName);
        sales.setSalePhone(customer.getCustomerPhone());
        sales.setSaleAddr(customer.getCustomerAddr());
        sales.setSaleCount(Integer.valueOf(saleCount));
        SalesService.addSales(sales);
        //TODO 销售订单入库
        BigDecimal totalPrice = product.getProductPrice();

        FinancialService financialService = new FinancialServiceImpl();
        Financial financial = new Financial();
        financial.setUpdatetime(new Date());
        financial.setIe(1);//未收
        financial.setMonth(AttendanceUtil.getMonth());
        financial.setKind(2);
        financial.setFname(saleName+"的订单");
        financial.setComment("无");
        financial.setAmount(totalPrice.multiply(new BigDecimal(saleCount)).doubleValue());
        UserLogin userLogin = (UserLogin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(userLogin.getUid());
        financial.setUserInfo(userInfo);
        Integer fid = financialService.addFinancial(financial);

        Order order = new Order();
        order.setOrderStatus(0);
        order.setOrderTag(2); //出货
        order.setOrderId(System.currentTimeMillis()/1000+"");
        order.setBusinessName(saleName);
        order.setBusinessAddr(customer.getCustomerAddr());
        order.setBusinessPhone(customer.getCustomerPhone());
        order.setOrderType(product.getCategoryType().toString());
        order.setTotalPrice(totalPrice.multiply(new BigDecimal(saleCount)));
        order.setFinanceId(fid);
        orderService.addOrder(order);


    }
    
    @RequestMapping("del")
    public void delSales(@RequestParam("id") Integer id){
        SalesService.deleteSalesById(id);
    }
    @RequestMapping("getAll")
    public ModelAndView getAll(Integer pageNum){
        ModelAndView modelAndView = new ModelAndView();
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Sales> SalesList = SalesService.findAll();
        PageInfo<Sales> SalesPageInfo = new PageInfo<Sales>(SalesList);
        modelAndView.addObject("pageInfo",SalesPageInfo);
        modelAndView.setViewName("/bssmanager/sales/salesList");
        return modelAndView;
    }

    @RequestMapping("salesProduct")
    public ModelAndView salesProduct(Integer pageNum){
        List<Customer> customerList = customerService.findAll();
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Product> productList = productService.findAll();
        PageInfo pageInfo = new PageInfo (productList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("customerList",customerList);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/bssmanager/sales/salesProduct");
        return modelAndView;
    }
    @RequestMapping("query")
    public ModelAndView query(String saleName,String saleAddr,String saleProduct){
        PageHelper.startPage(1,5);
        List<Sales> sales = SalesService.findSales(saleName, saleAddr, saleProduct);
        PageInfo pageInfo = new PageInfo (sales);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/bssmanager/sales/salesList");
        return modelAndView;
    }
}
